package org.ns.myblogs.service;

import org.ns.myblogs.dao.ArticleDao;
import org.ns.myblogs.pojo.ArticleBean;
import org.ns.myblogs.pojo.ResponseMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleService {

    @Autowired
    ArticleDao articleDao;

    public ResponseMap list(int pageNo, int size) {
        if (pageNo <= 0) {
            pageNo = 1;
        }
        Pageable pageable = PageRequest.of(pageNo - 1, size);
        Page<ArticleBean> beanPage = articleDao.findAll(pageable);
        int pages = beanPage.getTotalPages();
        long totalSize = beanPage.getTotalElements();
        List<ArticleBean> content = beanPage.getContent();
        return ResponseMap.newInstance()
                .putContent("list", content)
                .setPage(pageNo, pages, size, totalSize)
                .setMessage("获取文章列表成功");
    }

}
